package com.utils.info.util.hhy;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 返回时间戳和值
     *
     * @param key
     * @return
     */
    public Map<String, Object> get(String key) {
        String result = (String) redisTemplate.opsForValue().get(key);
        if (StrUtil.isNotEmpty(result)) {
            String[] resultArray = result.split("#");
            Map<String, Object> map = new HashMap();
            map.put("time", resultArray[0]);
            map.put("value", resultArray[1]);
            return map;
        }
        return null;
    }


    /**
     * 返回格式化的时间年-月-日 时:分:秒和值
     *
     * @param key
     * @return
     */
    public Map<String, Object> getFormatTime(String key) {
        String result = (String) redisTemplate.opsForValue().get(key);
        if (StrUtil.isNotEmpty(result)) {
            String[] resultArray = result.split("#");
            Map<String, Object> map = new HashMap();
            map.put("time", DateUtil.getTimeFromMin(Long.valueOf(resultArray[0])));
            map.put("value", resultArray[1]);
            return map;
        }
        return null;
    }


    public Double getPointValue(String key) {
        String result = (String) redisTemplate.opsForValue().get(key);
        if (StrUtil.isNotEmpty(result)) {
            String[] resultArray = result.split("#");
            Double value = Double.parseDouble(String.valueOf(resultArray[1]));
            return value;
        }
        return null;
    }


    /**
     * 根据key获取value值
     *
     * @param key
     * @return
     */
    public String getValue(String key) {
        String result = (String) redisTemplate.opsForValue().get(key);
        if (StrUtil.isNotEmpty(result)) {
            return result;
        }
        return null;

    }

    public String getValueFromInteger(String key) {
        String result = redisTemplate.opsForValue().get(key) == null ? null : String.valueOf(redisTemplate.opsForValue().get(key));
        if (StrUtil.isNotEmpty(result)) {
            return result;
        }
        return null;

    }


    /**
     * 插入redis
     *
     * @param key
     * @param value
     */
    public void setValue(String key, String value) {
        redisTemplate.opsForValue().set(key, value);

    }


    public void setValue(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);

    }

    /**
     * 插入redis
     *
     * @param key
     * @param value
     */
    public void setValue(String key, String value, int timeNum) {
        redisTemplate.opsForValue().set(key, value, timeNum, TimeUnit.MINUTES);

    }

    public void setValueForObject(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public void setValueForObject(String key, Object value, int timeNum) {
        redisTemplate.opsForValue().set(key, value, timeNum, TimeUnit.MINUTES);
    }

    public Object getValueForObject(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    public void convertAndSend(String channel, String msg) {
        redisTemplate.convertAndSend(channel, msg);
    }
}
